/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ejb.session;

import ejb.*;
import java.util.Collection;
import javax.ejb.*;
import javax.naming.*;

/**
 *
 * @author giulianoxt
 */
public class CadastroEmpresaBean implements SessionBean {
    private SessionContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;

    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations

    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }

    public void ejbCreate() { }
    
    // </editor-fold>;

  public void cadastrarFuncionario(String id) throws CreateException {
    try {
      Context ctx = new InitialContext();
      FuncionarioLocalHome home = (FuncionarioLocalHome)
        ctx.lookup("FuncionarioLocalHome");
      home.create(id);
    } catch (NamingException e) {
      e.printStackTrace();
    }
  }
  
  public void removerFuncionario(String id) throws CreateException {
    try {
      Context ctx = new InitialContext();
      FuncionarioLocalHome home = (FuncionarioLocalHome)
        ctx.lookup("FuncionarioLocalHome");

      Collection c = home.findById(id);
      FuncionarioLocal f = (FuncionarioLocal)c.iterator().next();
      f.remove();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void cadastrarGerente(String id) throws CreateException {
    try {
      Context ctx = new InitialContext();
      GerenteLocalHome home = (GerenteLocalHome)
        ctx.lookup("GerenteLocalHome");
      home.create(id);
    } catch (NamingException e) {
      e.printStackTrace();
    }
  }

  public void removerGerente(String id) throws CreateException {
    try {
      Context ctx = new InitialContext();
      GerenteLocalHome home = (GerenteLocalHome)
        ctx.lookup("GerenteLocalHome");

      Collection c = home.findById(id);
      GerenteLocal g = (GerenteLocal)c.iterator().next();
      g.remove();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void cadastrarCliente(String id) throws CreateException {
    try {
      Context ctx = new InitialContext();
      ClienteLocalHome home = (ClienteLocalHome)
        ctx.lookup("ClienteLocalHome");
      home.create(id);
    } catch (NamingException e) {
      e.printStackTrace();
    }
  }

  public void removerCliente(String id) throws CreateException {
    try {
      Context ctx = new InitialContext();
      ClienteLocalHome home = (ClienteLocalHome)
        ctx.lookup("ClienteLocalHome");

      Collection c = home.findById(id);
      ClienteLocal cl = (ClienteLocal)c.iterator().next();
      cl.remove();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
